<template>
  <div class="login">
    <Header>
      <span>登录</span>
    </Header>
    <section>
      <div class="iphone">
        <input
          type="text"
          placeholder="请输入手机号"
          v-model="telLogin"
          pattern="[0-9]*"
        />
      </div>
      <div class="password">
        <input type="text" placeholder="请输入密码" v-model="pwdLogin" />
      </div>
      <div class="log" @click="goLogin">登 录</div>
      <ul>
        <li @click="goUserLogin">
          <i class="iconfont icon-gongkaiyijianxiang"></i>
          <span>短信登陆</span>
        </li>
        <li @click="goRecovery">
          <i class="iconfont icon-gongkaiyijianxiang"></i>
          <span>找回密码</span>
        </li>
        <li @click="goRegister">
          <i class="iconfont icon-geren"></i>
          <span>快速注册</span>
        </li>
      </ul>
    </section>
  </div>
</template>

<script>
import { mapMutations } from "vuex";
import Header from "@/components/Login/Header.vue";
import { Toast } from "mint-ui";
import http from "@/common/request";
import { encrypt } from "@/common/rsaEncrypt";
export default {
  components: { Header },
  data() {
    return {
      telLogin: "",
      pwdLogin: "",
      rules: {
        telLogin: {
          rule: /^1[23456789]\d{9}$/,
          msg: "手机号不能为空,并且是11位数字",
        },
        pwdLogin: {
          rule: /^\w{6,12}$/,
          msg: "密码不能为空,并且是6-12位数字",
        },
      },
    };
  },
  methods: {
    goRecovery() {
      this.$router.push({
        path: "recovery",
      });
    },
    goRegister() {
      this.$router.push({
        path: "/register",
      });
    },
    ...mapMutations("user", ["user_login"]),
    goUserLogin() {
      this.$router.push({
        name: "UserLogin",
      });
    },
    async goLogin() {
      if (!this.validate("telLogin")) return;
      if (!this.validate("pwdLogin")) return;
      // 只有通过手机号和密码前端验证后才发送登录请求
      let pwd = encrypt(this.pwdLogin);
      console.log(pwd);
      let res = await http.$axios({
        url: "/api/login",
        method: "post",
        data: {
          userTel: this.telLogin,
          // userPwd: this.pwdLogin,
          userPwd: pwd,
        },
      });

      if (res.code == 301) {
        Toast(res.data.msg);
      } else if (res.code == 302) {
        Toast(res.data.msg);
      } else {
        Toast("登陆成功");
        // 把用户数据存入vuex
        // this.user_login(res.data)
        this.$store.commit("user/user_login", res.data);
        // 跳转到我的页面
        this.$router.push({
          name: "My",
        });
      }
      console.log(res);
    },
    validate(key) {
      let bool = true;
      if (!this.rules[key].rule.test(this[key])) {
        Toast(this.rules[key].msg);

        bool = false;
        return false;
      }
      return bool;
      // console.log(this.rules[key].rule);
    },
  },
};
</script>

<style scoped>
.login {
  height: 100vh;
  width: 100vw;
  background-color: #eee;
}

section {
  display: flex;
  flex-direction: column;
  align-items: center;
  margin-top: 40px;
}
.iphone,
.password {
  height: 40px;
  width: 80%;
  margin-bottom: 20px;
}
input {
  width: 100%;
  height: 100%;
  outline: none;
  padding-left: 10px;
}
.log {
  width: 80%;
  height: 40px;
  background-color: rgba(225, 0, 0, 1);
  border-radius: 10px;
  text-align: center;
  line-height: 40px;
  font-size: 18px;
  color: #eee;
}
ul {
  display: flex;
  justify-content: space-around;
  margin-top: 20px;
}
ul li {
  font-size: 16px;
  padding: 0 15px;
}
ul li i {
  padding-right: 5px;
}
</style>